package com.rui1024.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.rui1024.dao.SpecialtyDao;
import com.rui1024.dao.impl.SpecialtyDaoImpl;
import com.rui1024.pojo.Specialty;
import com.rui1024.util.JDBCUtil;


@WebServlet("/SpecialtyServlet")
public class SpecialtyServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
	
		request.setCharacterEncoding("utf-8");
		SpecialtyDao specialty = new SpecialtyDaoImpl();
		List<Specialty> list = new ArrayList<Specialty>();
		JDBCUtil jdbcUtil = new JDBCUtil();
		Connection con=null;
		try {
		con = jdbcUtil.getCon();
        String add_name = request.getParameter("add_name");

		if(add_name!=null){
			Specialty specialtya = specialty.find(add_name,con);
			list.add(specialtya);
			add_name="";
		}else{
			list=specialty.findAll(con);
		 }
		}
		catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					jdbcUtil.closeCon(con);
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		request.setAttribute("specialty", list);
		request.getRequestDispatcher("cc.jsp").forward(request, response);
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		doGet(request, response);
	}

}
